package models.weixin;

import jodd.bean.BeanCopy;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import models.UUIDModel;
import models.campus.Campus;
import models.constants.DeletedStatus;
import models.constants.Gender;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * 微信用户
 */
@Entity
@Table(name = "wx_weixin_users")
public class WeixinUser extends UUIDModel {

    private static final long serialVersionUID = 289109934185631L;
    public static final String WXMP_CACHEKEY  = "WxMpUser2_";

    /**
     * 微信用户名，fromUserName.
     * TODO: 加上索引.
     */
    @Column(name = "openId", length = 50)
    public String openId;


    /**
     * 用户属于哪一个微信号
     */
    @JoinColumn(name = "weixin_data")
    @ManyToOne
    public WeixinData weixinData;


//    /**
//     * 逻辑删除,0:未删除，1:已删除
//     */
//    @Enumerated(EnumType.STRING)
//    @Column(name = "from_type")
//    public UserFromType fromType = UserFromType.WEIXIN;
//
//
//    /**
//     * 用户身份  普通用户  商户
//     */
//    @Enumerated(EnumType.STRING)
//    @Column(name = "identity_type")
//    public IdentityType identityType = IdentityType.USER;


    /**
     * 认证授权码 本人/本商户 授权码 具有唯一性
     */
    @Column(name = "identity_code")
    public String identityCode;

    /**
     * 介绍人授权码  跟 identityCode 对应
     */
    @Column(name = "introduce_code")
    public String introduceCode;

    /**
     * 微信确认身份用
     */
    @Column(name = "uuid")
    public String uuid;


    /**
     * 是否订阅.
     * 用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息。
     * 可考虑通过一个定时任务查询当前公众号的用户订阅状态.
     */
    @Column(name = "subcribed")
    public Boolean subcribed;

    /**
     * 用户的昵称
     */
    @Column(name = "nick_name", length = 50)
    public String nickName;


    @Enumerated(EnumType.STRING)
    @Column(name = "sex", length = 10)
    public Gender sex;

    /**
     * 用户语言
     */
    @Column(name = "user_lang", length = 20)
    public String language;


    @Column(name = "city", length = 20)
    public String city;


    /**
     * 领取卡券次数
     */
    @Column(name = "give_coupon_count")
    public Integer giveCouponCount = 0;


    @Column(name = "country", length = 20)
    public String country;


    /**
     * 是否第一次进入
     */
    @Column(name = "is_first")
    public Boolean isFirst;

    /**
     * 用户头像路径.
     * <p/>
     * 保存微信文件到本地.
     */
    @Column(name = "head_img_url", length = 200)
    public String headImgUrl;




    @Column(name = "phone")
    public String phone;

    @Column(name = "birthDay")
    public String birthDay;

    @Column(name = "sign")
    public String sign;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;

    /**
     * 按weixinUser值更新指定ID的WeixinUser.
     */
    public static void update(Long id, WeixinUser weixinUser) {
        WeixinUser oldWeixinUser = WeixinUser.findById(id);
        BeanCopy.beans(weixinUser, oldWeixinUser).ignoreNulls(true).copy();
        oldWeixinUser.save();
    }

    /**
     * 删除指定ID的WeixinMenu
     */
    public static void delete(Long id) {
        WeixinUser weixinUser = WeixinUser.findById(id);
        weixinUser.deleted = DeletedStatus.DELETED;
        weixinUser.save();
    }

    /**
     * 根据OpenId查询用户
     */
    public static WeixinUser findByOpenId(String openId) {
        return WeixinUser.find("openId = ? and deleted = ?" , openId , DeletedStatus.UN_DELETED).first();
    }

    /**
     * 查询出商户指定OpenId的用户.
     */
    public static WeixinUser findOrCreateMerchantWxUser(Campus campus, WxMpUser wxMpUser , String dcode) {
        WeixinUser weixinUser = WeixinUser.find("weixinData = ? and openId=? and deleted = ?", campus.weixinData , wxMpUser.getOpenId() , DeletedStatus.UN_DELETED).first();
        if (weixinUser == null) {
            weixinUser = new WeixinUser();
            weixinUser.createdAt = new Date();
            weixinUser.deleted = DeletedStatus.UN_DELETED;
            weixinUser.weixinData = campus.weixinData;
            weixinUser.openId = wxMpUser.getOpenId();
            weixinUser.nickName = wxMpUser.getNickname();
            weixinUser.subcribed = Boolean.FALSE;
            weixinUser.introduceCode = dcode;
            weixinUser.city = wxMpUser.getCity();
            weixinUser.country = wxMpUser.getCountry();
            weixinUser.language = wxMpUser.getLanguage();
            weixinUser.headImgUrl = wxMpUser.getHeadImgUrl();
            weixinUser.isFirst = true;
            if ("1".equals(wxMpUser.getSex()) || "男".equals(wxMpUser.getSex())) {
                weixinUser.sex = Gender.MALE;
            } else if ("2".equals(wxMpUser.getSex()) || "女".equals(wxMpUser.getSex())) {
                weixinUser.sex = Gender.FEMALE;
            } else {
                weixinUser.sex = Gender.UNKNOWN;
            }
            weixinUser.subcribed = wxMpUser.getSubscribe();
            weixinUser.save();
        }
        weixinUser.save();
        return weixinUser;
    }

    public static List<WeixinUser> findAvailable() {
        return WeixinUser.find("deleted = ?" , DeletedStatus.UN_DELETED).fetch();
    }


}
